<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>课程二级分类列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索筛选 -->
        <el-form :inline="true" class="user-search">
            <el-form-item>
                <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
            </el-form-item>
        </el-form>

        <!--列表-->
        <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
            element-loading-text="拼命加载中" style="width: 100%;">

            <el-table-column align="center" type="index" label="序号" width="60">
            </el-table-column>

            <el-table-column prop="name" label="课程名称" width="300">
            </el-table-column>

            <el-table-column prop="sort" label="课程排序(值越小越靠前)" width="300">
            </el-table-column>

            <el-table-column prop="courseId" label="课程首页分类名称" width="300">
                <template slot-scope="scope">
                    <el-button size="mini" type="primary">{{ typeName(scope.row.courseId) }}</el-button>
                </template>
            </el-table-column>

            <el-table-column align="center" label="操作" min-width="300">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="deleteCategory(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 编辑界面 -->
        <el-dialog :title="title" :visible.sync="editFormVisible" width="50%" @click='closeDialog("edit")'>
            <el-form label-width="200px">
                <el-form-item label="课程二级分类名称" prop="name">
                    <el-input size="small" v-model="param.name" auto-complete="off" placeholder="请输入分类名称"></el-input>
                </el-form-item>

                <el-form-item label="首页分类类别">
                    <el-select :multiple="false" v-model="param.courseId" placeholder="请选择首页分类类别" style="width:240px;">
                        <el-option v-for="item in courseList" :key="item.name" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="课程分类排序(值越小越靠前)" prop="sort">
                    <el-input size="small" v-model="param.sort" auto-complete="off" placeholder="请输入排序"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
                <el-button size="small" type="primary" :loading="loading" class="title" @click="addCategory()"
                    v-if="title == '添加'">添加</el-button>
                <el-button size="small" type="primary" :loading="loading" class="title" @click="upCategory()"
                    v-if="title == '修改'">修改</el-button>
            </div>
        </el-dialog>

    </div>
</template>
<script>

import {
    addCourse, selectCourseByCategoryTypes, updateCourseByIds,deleteCourse
} from "../../api/userMG";

import Pagination from "../../components/Pagination";
export default {
    data() {
        return {
            loading: false, //是显示加载
            editFormVisible: false, //控制编辑页面显示与隐藏
            menuAccessshow: false, //控制数据权限显示与隐藏
            title: "添加",
            name: "",
            courseList: [],
            // 删除
            seletedata: {
                ids: "",
                token: localStorage.getItem("logintoken"),
            },
            listData: [], //用户数据

            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 10,
                total: 10,
            },
            param: {
                name: '雅思一班',
                sort: '0',
                indexId: '',
                categoryType: '1',
                courseId: '',
            }
        };
    },
    // 注册组件
    components: {
        Pagination,
    },
    /**
     * 数据发生改变
     */
    watch: {},
    computed: {
        typeName() {
            return typeId => {
                let name = ''
                this.courseList.forEach(item => {
                    if (item.id == typeId) {
                        name = item.name
                    }
                })
                return name
            }
        }
    },
    /**
     * 创建完毕
     */
    created() {
        this.getdata()
        this.getCourseTypeLst()
    },
    /**
     * 里面的方法只有被调用才会执行
     */

    methods: {
        // 添加分类
        addCategory() {
            this.loading = true;

            this.param.categoryType = 1

            let arr = this.courseList.filter(item => {
                return item.id == this.param.courseId
            })

            this.param.indexId = arr[0].indexId


            addCourse({
                ...this.param
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.editFormVisible = false
                    this.$message({
                        type: "success",
                        message: res.msg,
                    });
                    this.getdata();
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        // 修改
        upCategory() {
            this.loading = true;
            this.param.categoryType = 1

            let arr = this.courseList.filter(item => {
                return item.id == this.param.courseId
            })

            this.param.indexId = arr[0].indexId
            updateCourseByIds({
                ...this.param
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.editFormVisible = false
                    this.$message({
                        type: "success",
                        message: res.msg,
                    });
                    this.getdata();
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        getCourseTypeLst() {
            selectCourseByCategoryTypes({
                categoryType: 0,
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.courseList = res.data.data;
                    return;
                }
                this.$message({
                    type: "info",
                    message: res.msg,
                });
            });
        },
        // 获取数据方法
        getdata() {

            this.loading = true;

            selectCourseByCategoryTypes({
                categoryType: 1,
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.listData = res.data.data;

                    return;
                }
                this.$message({
                    type: "info",
                    message: res.msg,
                });
            });
        },
        // 分页插件事件
        callFather(parm) {
            this.formInline.page = parm.currentPage;
            this.formInline.limit = parm.pageSize;
            this.getdata(parm.currentPage);
        },
        //显示编辑界面
        handleEdit: function (row) {
            this.editFormVisible = true;
            if (row != undefined && row != "undefined") {
                this.title = "修改";
                this.param.id = row.id;
                this.param.name = row.name;
                this.param.sort = row.sort;
                this.param.indexId = row.indexId;
                this.param.categoryType = row.categoryType;
                this.param.courseId = row.courseId;
            } else {
                this.title = "添加";
                this.param.name = "";
                this.param.sort = "";
                this.param.indexId = "";
                this.param.categoryType = "";
                this.param.courseId = "";
            }
        },
        // 删除
        deleteCategory(index, row) {
            this.$confirm("确定要删除吗?", "信息", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    deleteCourse({ id: row.id })
                        .then((res) => {
                            if (res.code == 200) {
                                this.$message({
                                    type: "success",
                                    message: "已删除！",
                                });
                                this.getdata();
                            } else {
                                this.$message({
                                    type: "info",
                                    message: res.msg,
                                });
                            }
                        })
                        .catch((err) => {
                            this.loading = false;
                            this.$message.error("删除失败，请稍后再试！");
                        });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },

        // 关闭编辑、增加弹出框
        closeDialog(dialog) {
            if (dialog == "edit") {
                this.editFormVisible = false;
            } else if (dialog == "perm") {
                this.menuAccessshow = false;
            }
        },
    },
};
</script>

<style scoped>
.user-search {
    margin-top: 20px;
}

.userRole {
    width: 100%;
}
</style>